define(function(require, exports){
	var pubjs = require('pubjs');
	var baseGrid	= require('@base/grid').baseNoDate;
	var $ = require('jquery');
	var util = require('util');
	var labels = require('@sys_labels').labels;

	var Spot = baseGrid.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'cols': [
					{"type":"id"}
					,{"name":"Name","text":LANG("广告位"),"type":"index","width":210,render:'renderName'}
					,{"name":"DealId","text":LANG("所属订单ID"),"type":"index"}
					,{"name":"Size","text":LANG("尺寸"),"type":"index"}
				]
				,'hasSearch': false
				,'hasTab': false
				,'hasExport': false
				,'hasAmount': false
				,'hasPager': false
				,'hasSelect': true
				,'url': 'deal/spots'
				,'sub_field': 'key'
				,'reqType': 'websocket'
				,'param' :{
					'noAmount': true
					,'Status': 1
				}
				,'list':{
					'rowSelect': true,
					'scroll_type': 'row',
					'scroll_size': 8
				}
			});

			this.Super('init', arguments);
		},
		renderName: function(i, val, data,con){
			return LANG(val) || '-';
		}
	});

	exports.spot = Spot;

	var List = baseGrid.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'cols': [
					{'type':'select', 'name':'sel', 'all':1, width:50},
					{"name":"_id","text":LANG("ID"),"type":"index","width":210},
					{name: 'Name', type:'index',render:'renderName',width:200},
					{name: 'Time', text: LANG('订单时间'), type:'index',render:'renderDealTime',width:200},
					{name: 'Status', text:LANG('状态'), render:'renderStatus', align:'center'},
					{name: 'channelName', text:LANG('渠道'), align:'center'},
					{name: 'SpotsInfo', text:LANG('广告位数量'), render:'renderCount',align:'center'},
					{name: 'UpdateTime', text:LANG('更新时间'), render: 'renderTime',align:'center'},
					{name: 'operation', type:'op', render:'renderOperation', width: 100}
				]
				,'hasSearch': false
				,'hasTab': false
				,'hasExport': false
				,'hasAmount': false
				,'hasPager': false
				,'url': 'deal/list'
				,'sub_field': '_id'
				,'reqType': 'websocket'
				,'pager': {'size':10}
				,'param' :{
					Status: 1
				}
				,opClick: true
			});

			this.Super('init', arguments);
		}
		,renderOperation: function(idx, val, row) {
			var ops = [
				'<a data-op="sync" >'+LANG("同步")+'</a>',
				'<a data-op="edit" >'+LANG("编辑")+'</a>'
			];

			return ops.join(' | ');
		}
		,onListOpClick: function(ev){
			var self = this
			var param = ev.param;
			var data = param.data;
			switch (param.op){
				case 'sync':
					pubjs.loading.show();
					pubjs.mc.send('deal/sync', {_id: data._id, AdxId: data.AdxId}, self.afterSync.bind(self));
					break;
				case 'edit':
					window.open('/#admin/deal/edit/'+ data._id + '?Type=' + data.Type);
					break;
			}

			return false;
		}
		,afterSync: function(err, data) {
			pubjs.loading.hide();
			if(err) {
				pubjs.alert(err.message);
				return false;
			}

			pubjs.alert(data && data.message || LANG('同步成功'));
		}
		,renderPrice: function(i, val, data) {
			var price = data.DealInfo && data.DealInfo.Price || 0;
			return labels.formatCurrencyKeep2(price);
		}
		,renderDealTime: function(i, val, data) {
			var info = data && data.DealInfo;
			var ret = '';
			var begin, end;
			if(info) {
				begin = info.BeginTime ? util.date("Y-m-d", info.BeginTime) : null;
				end = info.EndTime ? util.date("Y-m-d", info.EndTime) : null;
			}

			ret += begin ? begin : LANG('未知');
			end = end ? end : LANG('未知');
			ret += ' - ' + end;
			return ret ? ret : '不限';
		}
		,renderTime: function(i, val) {
			return val ? util.date("Y-m-d H:i:s", val) : null;
		}
		,renderStatus: function(i,val,data,con){
			var text;
			switch (+val) {
				case 1:
					text = '开启';
					break;
				case 2:
					text = '关闭';
					break;
				default :
					text = '-';
			}
			return LANG(text);
		}
		,renderName: function(i, val, data,con){
			val = LANG(val) || LANG('-');
			return $('<span class="M-tableListWidthLimit"/>')
				.attr({'title':val})
				.text(val).width(con.width);
		}
		,renderCount: function(idx, val, row, col){
			return (val && val.length || 0).toString();
		}

	});

	exports.list = List;

});